<?php

namespace App\Modules\Index\Services;


use  App\Modules\Index\Models\diningTime;
use DB;

class diningTimeService
{

    /**
     * 添加血糖
     * param array $data
     */
    public static function mealsTime($user_id){

        $data['mealsconf'] = diningTime::where('user_id',$user_id)->get()->toArray();
        $data['basic'] = DB::table('users')->where('id',$user_id)->select('height','weight')->get()->toArray();
        if(empty($data['mealsconf'])){

            $data['mealsconf'] = diningTimeService::defaultMeals();
        }

        return $data;


    }

    public static function defaultMeals(){
        $res = DB::table('dictionary')->where('key','standard')->orWhere('key','diningtime')->orderBy('key','asc')->get()->toArray();
        $arr = [];
        foreach($res as $k => $val){
            $arr[$val->key] = DB::table('dictionary')->where('dictionary_id',$val->id)->orderBy('key','asc')->get()->toArray();
        }

        $before_low = substr($arr['standard']['0']->value,0,strpos($arr['standard']['0']->value, '-'));
        $before_high = substr($arr['standard']['0']->value,strpos($arr['standard']['0']->value, '-')+1);
        $after_low = substr($arr['standard']['1']->value,0,strpos($arr['standard']['1']->value, '-'));
        $after_high = substr($arr['standard']['1']->value,strpos($arr['standard']['1']->value, '-')+1);
        $bad_low = substr($arr['standard']['2']->value,0,strpos($arr['standard']['2']->value, '-'));
        $bad_high = substr($arr['standard']['2']->value,strpos($arr['standard']['2']->value, '-')+1);

        $data[0]['meals'] = 1;
        $data[0]['start_time'] = substr($arr['diningtime']['0']->value,0,strpos($arr['diningtime']['0']->value, '-'));
        $data[0]['end_time'] = substr($arr['diningtime']['0']->value,strpos($arr['diningtime']['0']->value, '-')+1);
        $data[0]['before_low'] = $before_low;
        $data[0]['before_high'] = $before_high;
        $data[0]['after_low'] = $after_low;
        $data[0]['after_high'] = $after_high;

        $data[1]['meals'] = 2;
        $data[1]['start_time'] = substr($arr['diningtime']['1']->value,0,strpos($arr['diningtime']['1']->value, '-'));
        $data[1]['end_time'] = substr($arr['diningtime']['1']->value,strpos($arr['diningtime']['1']->value, '-')+1);
        $data[1]['before_low'] = $before_low;
        $data[1]['before_high'] = $before_high;
        $data[1]['after_low'] = $after_low;
        $data[1]['after_high'] = $after_high;

        $data[2]['meals'] = 3;
        $data[2]['start_time'] = substr($arr['diningtime']['2']->value,0,strpos($arr['diningtime']['2']->value, '-'));
        $data[2]['end_time'] = substr($arr['diningtime']['2']->value,strpos($arr['diningtime']['2']->value, '-')+1);
        $data[2]['before_low'] = $before_low;
        $data[2]['before_high'] = $before_high;
        $data[2]['after_low'] = $after_low;
        $data[2]['after_high'] = $after_high;

        $data[3]['meals'] = 4;
        $data[3]['start_time'] = substr($arr['diningtime']['3']->value,0,strpos($arr['diningtime']['3']->value, '-'));
        $data[3]['end_time'] = substr($arr['diningtime']['3']->value,strpos($arr['diningtime']['3']->value, '-')+1);
        $data[3]['before_low'] = $bad_low;
        $data[3]['before_high'] = $bad_high;

        return $data;
    }

    /**
     * 判断餐别
     */
    public static function userMeals($user_id,$time){
        //查询用户的基本配置
        $date =date('H:i:s',strtotime($time));//当前时间
        $result = diningTimeService::mealsTime($user_id);
        $meals = $result['mealsconf'];

        //区间范围判断
        if(strtotime($meals[0]['start_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['0']['end_time']) ){
            if(floor((strtotime($meals['0']['start_time']) + strtotime($meals['0']['end_time']))/strtotime($date)) > 1 ) return 1;
            else return 2;
        }

        if(strtotime($meals[0]['end_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['1']['start_time']) ){
            if(floor((strtotime($meals['0']['end_time']) + strtotime($meals['1']['start_time']))/strtotime($date)) > 1 ) return 2;
            else return 3;
        }

        if(strtotime($meals[1]['start_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['1']['end_time']) ){
            if(floor((strtotime($meals['1']['start_time']) + strtotime($meals['1']['end_time']))/strtotime($date)) > 1 ) return 3;
            else return 4;
        }

        if(strtotime($meals[1]['end_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['2']['start_time']) ){
            if(floor((strtotime($meals['1']['end_time']) + strtotime($meals['2']['start_time']))/strtotime($date)) > 1 ) return 4;
            else return 5;
        }

        if(strtotime($meals[2]['start_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['2']['end_time']) ){
            if(floor((strtotime($meals['2']['start_time']) + strtotime($meals['2']['end_time']))/strtotime($date)) > 1 ) return 5;
            else return 6;
        }

        if(strtotime($meals[2]['end_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['3']['start_time']) ){
            if(floor((strtotime($meals['2']['end_time']) + strtotime($meals['3']['start_time']))/strtotime($date)) > 1 ) return 6;
            else return 7;
        }
        if($meals['3']['end_time'] == '00:00') $meals['3']['end_time'] = '23:59';

        if(strtotime($meals[3]['start_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['3']['end_time']) ){
            return 7;
        }

        return 1;

    }

    /**
     *用户餐别和当前时间的血糖范围
     */
    public static function userMealsNormal($user_id,$time){
        //当前时间的餐别
        $meals = self::userMeals($user_id,$time);
        $data = self::mealsTime($user_id);
//        p($data);
        $arr['meals']  = $meals;
        $mealsconf = $data['mealsconf'];
        foreach($mealsconf as $key => $value){
            if($meals == 1 || $meals ==2){
                $str = 1;
            }else if($meals == 3 || $meals ==4){
                $str = 2;
            }else if($meals == 5 || $meals ==6){
                $str = 3;
            }else if($meals == 7){
                $str = 4;
            }
            if($str == $value['meals']){
//                $arr['glu']  = $value;
                if($meals%2 == 0){
                    $arr['low'] = $value['after_low'];
                    $arr['high'] = $value['after_high'];
                }else{
                    $arr['low'] = $value['before_low'];
                    $arr['high'] = $value['before_high'];
                }
                break;
            }
        }

        return $arr;

    }

}